# 数据库工具类
# read config
import configparser
import mysql.connector

from app.utils.config_reader import ConfigReader


class DAO:


    def __init__(self):
        config_reader = ConfigReader()
        self.data_base = config_reader.get_value('database_dev', 'database')
        self.usr_name = config_reader.get_value('database_dev', 'username')
        self.usr_pwd = config_reader.get_value('database_dev', 'userpwd')



    # 连接数据库
    def connect_db(self):
        conn = mysql.connector.connect(user=self.usr_name, password=self.usr_pwd, database=self.data_base)
        cursor = conn.cursor()
        return conn, cursor

    # 执行查询
    def execute_query(self, sql):
        conn, cursor = self.connect_db()
        cursor.execute(sql)
        values = cursor.fetchall()
        cursor.close()
        conn.close()
        return values

    # 执行插入
    def execute_insert(self, sql):
        conn, cursor = self.connect_db()
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()

    def execute_update(self, sql):
        conn, cursor = self.connect_db()
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()

    def test_qurey(self):
        sql = 'select * from fund_mkt'
        values = self.execute_query(sql)
        print(values)


if __name__ == "__main__":
    dao_utils = DAO()
    dao_utils.test_qurey()

